package com.bite.redisDemo;

import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.params.SetParams;

import java.util.Set;

@Slf4j
public class RedisDemoGeneric {
    public static void test1(Jedis jedis){
        System.out.println("get 和 set 的使用");
        // 现清空之前的key避免产生影响
        jedis.flushAll();
//        jedis.set("key1","111");
        jedis.set("key2","222");
//         String value1 = jedis.get("key1");
 //        System.out.println("key1: "+value1);
        SetParams params = new SetParams();
        // 设置过期时间
        params.ex(10);
        // 设置 nx(不存在则创建,存在则不改变) / xx(存在才修改,不存在则不创建)
        params.xx();
        jedis.set("key1","333",params);
        System.out.println("xx修改处理后 key1: "+jedis.get("key1"));
        String value2 = jedis.get("key2");
        System.out.println("key2: " + value2);
    }

    public static void test2(Jedis jedis){
        System.out.println("del 和 exists");
        jedis.flushAll();
        jedis.set("key1","111");
        jedis.set("key2","222");
        boolean key1Exists = jedis.exists("key1");
        System.out.println(key1Exists);
        // 可以同时删除多个 可变长参数
        jedis.del("key1","key2");
        System.out.println("del后");
        boolean key1After = jedis.exists("key1");
        boolean key2After = jedis.exists("key2");
        System.out.println("key1: " + key1After + " key2: " + key2After);
    }

    public static void test3(Jedis jedis){
        System.out.println("keys * 的使用");
        jedis.flushAll();
        jedis.set("key1","111");
        jedis.set("key2","111");
        jedis.set("key3","111");
        jedis.set("key4","444");
        // redis中的key不能重复,而且也是不在意顺序的,所以用set更合适
        Set<String> keys = jedis.keys("*");
        System.out.println(keys);
    }

    public static void test4(Jedis jedis){
        System.out.println("expire 和 ttl");
        jedis.flushAll();
        jedis.set("key1","111");
        jedis.expire("key1",4);
        long time = jedis.ttl("key1");
        System.out.println("过期时间: "+time+"s");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        long timeAfterThread3000 = jedis.ttl("key1");
        System.out.println("ThreadSleep3000: "+timeAfterThread3000+"s");
        try {
            Thread.sleep(1001);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        long timeExpire = jedis.ttl("key1");
        System.out.println("过期了,ttl返回值: "+timeExpire);
    }

    public static void test5(Jedis jedis){
        System.out.println("type 的使用");
        jedis.flushAll();

        jedis.set("key1","111");
        String type1 = jedis.type("key1");
        System.out.println("type: "+type1);

        jedis.lpush("key2","111","222","333");
        String type2 = jedis.type("key2");
        System.out.println("type: "+type2);

        jedis.hset("key3","f1","111");
        String type3 = jedis.type("key3");
        System.out.println("type: "+type3);

        jedis.sadd("key4","111","222","333");
        String type4 = jedis.type("key4");
        System.out.println("type: "+type4);

        jedis.zadd("key5",10,"zhangsan");
        String type5 = jedis.type("key5");
        System.out.println("type: "+type5);



    }


    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try(Jedis jedis = jedisPool.getResource()) {
            String pong = jedis.ping();
            System.out.println(pong);
//            System.out.println(pong);
//            test1(jedis);
//            test2(jedis);
//            test3(jedis);
//            test4(jedis);

            test5(jedis);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
